/**
 * Created by Tim on 2017/5/23.
 */
$(function () {
    function OrderMgr() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');

        // 初始化
        this.init();
    }

    OrderMgr.prototype.Constructor = OrderMgr;

    OrderMgr.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    OrderMgr.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource": "/fee/page",
            "aoColumns": [
                {"sWidth": "15%", "sTitle": "订单号", "sClass": "center", "mData": "orderNo", "bSortable": false},
                {"sWidth": "15%", "sTitle": "金额", "sClass": "center", "mData": "amount", "bSortable": false},
                {"sWidth": "8%", "sTitle": "车牌号", "sClass": "center", "mData": "carNo", "bSortable": false},
                {"sWidth": "15%", "sTitle": "用户微信昵称", "sClass": "center", "mData": "nickName", "bSortable": false},
                {"sWidth": "15%", "sTitle": "用户手机号", "sClass": "center", "mData": "userMobile", "bSortable": false},
                {"sWidth": "15%", "sTitle": "创建时间", "sClass": "center", "mData": "created", "bSortable": false},
                {"sWidth": "8%", "sTitle": "状态", "sClass": "center", "mData": "status", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];

                if (aData['amount']) {
                    $('td:eq(1)', nRow).html(aData['amount'] + "￥");
                }

                if (aData['created']) {
                    $('td:eq(5)', nRow).html(SysTool.Date.formatDate(aData['created'], 'yyyy-MM-dd hh:mm:ss'));
                }
                var state = aData['status'];
                if (state === 1) {
                    $('td:eq(6)', nRow).html('<span class="label label-primary arrowed">未支付</span>');
                } else if (state === 2) {
                    $('td:eq(6)', nRow).html('<span class="label label-success arrowed">已支付</span>');
                }
                // 操作
                // var option = '<a title="管理用户权限" href="/users/' + id + '/roles" class="btn btn-xs btn-primary">权限</a>';
                // option += '&nbsp;<a title="修改用户信息" href="/users/' + id + '/edit" class="btn btn-xs btn-info">编辑</a>';
                // if (state === 1) {
                //     option += '&nbsp;<a title="禁用用户" userId="' + id + '" href="javascript:;" class="btn btn-xs btn-warning">禁用</a>';
                // } else {
                //     option += '&nbsp;<a title="启用用户" userId="' + id + '" href="javascript:;" class="btn btn-xs btn-success">启用</a>';
                // }
                // option += '&nbsp;<a title="删除用户" userId="' + id + '" href="javascript:;" class="btn btn-xs btn-danger">删除</a>';
                // $('td:eq(6)', nRow).html(option);
            },
            "fnServerParams": function (aoData) {
                queryCondition("#search-form", aoData);
            },
            "fnStateLoaded": function (oSettings, oData) {
//                alert(JSON.stringify(oData));
            }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
            // alert($("#propertyMgrId").val());
            // alert($("#marketId").val());
            // var propertyMgr = {name: "propertyMgrId", value: $("#propertyMgrId").val()};
            // var market = {name: "marketId", value: $("#marketId").val()};
            // aoData.push(propertyMgr);
            // aoData.push(market);
        }
    };

    OrderMgr.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    OrderMgr.prototype.registEvent = function () {
        var that = this;
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.$btn_search_clear.click(function () {
            $('#orderNo').val('');
            $('#carNo').val('');
            $('#begin').val('');
            $('#end').val('');
            that.$seachr_btn.click();
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.OrderMgr = new OrderMgr();
});